﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace BillGuru.Persistence
{
    class Product
    {
        public Product(BillGuruDataSetTableAdapters.ProductTableAdapter productAdapter, BillGuru.BillGuruDataSet data)
        {
            this.productAdapter = productAdapter;
            this.data = data;

            this.productAdapter.Adapter.RowUpdated += new System.Data.OleDb.OleDbRowUpdatedEventHandler(OnRowUpdated);
        }

        public void OnRowUpdated(object source, System.Data.OleDb.OleDbRowUpdatedEventArgs args)
        {
            SharedEventHandlerImpl.RetrieveKeyOnRowInsert(source, args);
        }

        public void saveChanges()
        {
           productAdapter.Update(data.Product);
        }

        public void Fill()
        {
            this.productAdapter.Fill(this.data.Product);
        }

        public void FillByDescription(string sFilter)
        {
            if (sFilter == string.Empty)
            {
                System.Windows.Forms.MessageBox.Show("Please enter a string to look for");
                return;
            }

            string sCmd;
            sCmd = "Select * from Product Where ";
            sCmd += BillGuru.Utilities.DatabaseUtilities.createProductDescriptionFilter(sFilter);
            using(System.Data.OleDb.OleDbConnection con = BillGuru.Utilities.DatabaseUtilities.createConnection())
            {
                productAdapter.Adapter.SelectCommand = new System.Data.OleDb.OleDbCommand(sCmd, con);
                data.Product.Clear();
                productAdapter.Adapter.Fill(data.Product);
            }

        }

        private BillGuruDataSetTableAdapters.ProductTableAdapter productAdapter;
        private BillGuru.BillGuruDataSet data;

    }
}
